﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.IO;

namespace FilteredIndustrialGrid
{
    public class FilteredIndustrialGrid : WebPart
    {
        private string _storeProcedure, _url, _ddpSp, _ddpFilter;

        public enum pickerEnum
        {
            None,
            CalendarPicker,            
            DropDownPicker,
        };
        protected pickerEnum _myEnum;
        
        public enum colorScheme
        {
            Blue,
            Orange,
            Green,
        };
        protected colorScheme _colorScheme;

        protected override void CreateChildControls()
        {            
            base.CreateChildControls();            
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            if (_url == "" || _url == null || _storeProcedure == "" || _storeProcedure == null)
            {
                writer.Write(
                    "<p id=\"salida\"></p> " +
                    "<center>"+
                        "<script language=\"javascript\" type=\"text/javascript\">" +
                        "    var salida = document.getElementById('salida'); salida.style.color = 'Red'; salida.style.fontFamily = 'Tahoma'; salida.style.fontSize = '12';"+
                        "    salida.innerHTML = 'Please enter a valid url and store procedure name in Data Source properties of the web part.'; " +
                        "</script>"+
                    "</center>"
                );
            }
            else
            {                               
                writer.Write(
                    "<link type=\"text/css\" href=\"" + _url + "/Styles/IndustrialDashboard.css\" rel=\"stylesheet\" />\n" +
                    "<link id=\"colorLink\" type=\"text/css\" href=\"" + _url + "/Widgets/IndustrialGrid/Styles/IndustrialGrid.css\" rel=\"stylesheet\"  />    \n" +
                    "<link type=\"text/css\" href=\"" + _url + "/Widgets/DropDownPicker/Styles/DropDownPicker.css\" rel=\"stylesheet\" />\n" +
                    "<link type=\"text/css\" href=\"" + _url + "/Widgets/CalendarPicker/Styles/CalendarPicker.css\" rel=\"stylesheet\" />    \n" +    

                    "<script src=\""+_url+"/Scripts/IndustrialDashboard.js\" type=\"text/javascript\"></script>\n"+
                    "<script src=\"" + _url + "/Widgets/DropDownPicker/Scripts/DropDownPicker.js\" type=\"text/javascript\"></script>        \n" +
                    "<script src=\"" + _url + "/Widgets/CalendarPicker/Scripts/CalendarPicker.js\" type=\"text/javascript\"></script>        \n" +
                    "<script src=\"" + _url + "/Widgets/IndustrialGrid/Scripts/IndustrialGrid.js\" type=\"text/javascript\"></script>        \n" +

                    "<style type=\"text/css\">\n"+
                        ".calendarPickerMasterClass {width:178px;position:absolute;float:left;}\n " +
                        ".calendarPickersContainers .inputscalendarPickers {height:20px;width:178px;padding:0px;margin:0px;left:0px;}        \n " +
                        ".calendarPickersContainers .inputscalendarPickersFocus {height:20px;width:178px;padding:0px;margin:0px;left:0px;}\n " +
                        ".calendarPickersContainers .dpCal{margin:0px;padding:0px;top:2px;left:-89px;}    \n "+
                    "</style>\n"+
                
                    "<center>\n"+
                    "    <div id=\"mainDiv\" style=\"margin:0px;padding:5px;border:0px;\"> \n" +
                    "            <div id=\"pReportContainer\" class=\"igrc\"> \n" +
                    "                <div id=\"pIG\" style=\"width:100%;margin-right:15px;\"></div> \n" +
                    "            </div>\n"+
                    "    </div>\n"+
                    "</center>   \n"+

                    "<script language=\"javascript\" type=\"text/javascript\">    \n"+

                       "var url = '"+_url+"';\n"+
                       "var sProcedure = '"+_storeProcedure+"';\n"+
                       "var pickerChoice = '"+_myEnum+"';\n"+
                       "var ddpSp = '"+_ddpSp+"';\n"+
                       "var ddpFilter = '"+_ddpFilter+"';\n"+
                       "var svcUrl = url + '/DALService.svc/CallProcedure';   \n" +
                       "var colorSchemeChoice = '" + _colorScheme + "';\n" +

                        "if(colorSchemeChoice == 'Orange') {\n"+
                        "var link = document.getElementById('colorLink'); \n"+
                            "link.href = '"+_url+"/Widgets/IndustrialGrid/Styles/IndustrialGridOrange.css'; \n" +
                        "} \n"+
                        "if(colorSchemeChoice == 'Green') {\n" +
                        "var link = document.getElementById('colorLink'); \n" +
                            "link.href = '" + _url + "/Widgets/IndustrialGrid/Styles/IndustrialGridGreen.css'; \n" +
                        "} \n" +

                        "attachDomReadyEventHandler(function () {                                             \n"+
                        "   var ig = new IndustrialGrid(document.getElementById('pIG'), \n"+
                        "                        { 'URL': svcUrl, 'Procedure': sProcedure});           \n"+

                        "   switch(pickerChoice) { \n"+
                        "        case \"None\" : { \n"+
                        "           var SQLParams = new SQLParameters(); \n"+
                        "            ig.refreshData(SQLParams);    \n"+
                        "            break; \n"+
                        "        }                \n"+
                        "        case \"DropDownPicker\" : { \n"+
                        "            if(ddpSp == null || ddpFilter == null || ddpSp == \"\" || ddpFilter == \"\") {       \n"+                      
                        "                var salida = document.createElement('p'); salida.style.color = \"Red\"; salida.style.fontFamily = \"Tahoma\"; salida.style.fontSize = \"12\"; \n"+
                        "                salida.innerHTML = \"Please enter a valid store procedure and filter for the DropDown Picker.\"; \n"+
                        "                document.getElementById('mainDiv').appendChild(salida);                             \n"+
                        "            } else { \n"+
                        "               try { \n"+
                        "                    document.getElementById('pIG').style.marginTop = '10px'; "+
                        "                    var ddpCont = document.createElement('div'); \n"+
                        "                    ddpCont.id = 'pickerContainer'; \n"+              
                        "                    document.getElementById('mainDiv').insertBefore(ddpCont, document.getElementById('pReportContainer')); \n"+

                        "                    if(ddpFilter != \"\" && ddpFilter != null) { \n"+
                        "                        var ddpFilterArray = ddpFilter.split(\",\"); \n"+
                        "                    } \n"+                                            
                        "                    var ddp = new DropDownPicker(ddpCont, \n"+
                        "                                                { 'URL': svcUrl, 'Procedure': ddpSp \n"+
                        "                                                    , 'events': { 'onComplete': refreshIndustrialGrid \n"+
                        "                                                                , 'onChange': refreshIndustrialGrid \n"+
                        "                                                    } \n"+
                        "                                                });     \n"+                        
                        "                    ddp.refreshData();                    \n"+
                        "                        function refreshIndustrialGrid() { \n"+
                        "                            try {                               \n"+          
                        "                                ig.refreshData(ddp.getResults(ddpFilterArray)); \n"+                    
                        "                            } \n"+
                        "                            catch (Error) { } \n"+
                        "                       }                       \n"+
                        "                 } catch(Error) {}       \n"+
                        "            } \n"+
                        "            break; \n"+
                        "        }     \n"+
                        "        case \"CalendarPicker\" : { \n"+
                        "            try { \n"+
                        "                document.getElementById('pIG').style.marginTop = '35px'; \n" +
                        "                var d1 = document.createElement('div'); \n"+                        
                        "                d1.id = \"pCalendarPicker\"; \n"+      
                        "                d1.className = \"calendarPickerMasterClass\";"+                         
                        "                document.getElementById('mainDiv').insertBefore(d1, document.getElementById('pReportContainer')); \n"+
                        "                var cp = new CalendarPicker(d1, { 'URL': svcUrl, 'Procedure': sProcedure \n"+
                        "                                                    , 'events': { 'onComplete': refreshCalendarPicker \n"+
                        "                                                                , 'onChange': refreshCalendarPicker \n"+
                        "                                                    } \n"+
                        "                                                });   \n"+

                        "                    function refreshCalendarPicker() { \n"+
                        "                        try {                        \n"+
                        "                            ig.refreshData(cp.getResults()); \n"+                    
                        "                        } \n"+
                        "                        catch (Error) { } \n"+
                        "                    } \n"+
                        "            } catch(Error) {} \n"+
                        "            break; \n"+
                        "        }       \n"+
                        "     }               \n"+      
               "});                               \n"+
               "</script>"
                ); //from c#                              
            }//from c#
            RenderChildren(writer);
        }

        [WebBrowsable(true)
        , Personalizable(true)
        , Category("Data Source")
        , WebDisplayName("Store Procedure name")
        , WebDescription("Type store procedure name here")
        , DefaultValue("")
        ]
        public string storeProcedure
        {
            get { return _storeProcedure; }
            set { _storeProcedure = value; }
        }

        [WebBrowsable(true)
        , Personalizable(true)
        , Category("Data Source")
        , WebDisplayName("IndustrialDashboard root url")
        , WebDescription("Type the url where IndustrialDashboard is installed")
        , DefaultValue("")
        ]
        public string url
        {
            get { return _url; }
            set { _url = value; }
        }

        [Category("Data Source")
        , DefaultValue(pickerEnum.None)
        , Description("Select a value from the dropdown list")
        , WebBrowsable(true)
        , Personalizable(true)
        , WebDisplayName("Picker to use")]
        public pickerEnum pickerChoice
        {
            get { return _myEnum; }
            set { _myEnum = value; }
        }

        [WebBrowsable(true)
        , Personalizable(true)
        , Category("Data Source")
        , WebDisplayName("(only for DropDown Picker) Dropdown picker Store Procedure Name")
        , WebDescription("Type the Dropdown picker Store Procedure name")
        , DefaultValue("")
        ]
        public string ddpSp
        {
            get { return _ddpSp; }
            set { _ddpSp = value; }
        }

        [WebBrowsable(true)
        , Personalizable(true)
        , Category("Data Source")
        , WebDisplayName("(only for DropDown Picker) Dropdown picker filter")
        , WebDescription("Type the items to filter separated by commas")
        , DefaultValue("")
        ]
        public string ddpFilter
        {
            get { return _ddpFilter; }
            set { _ddpFilter = value; }
        }

        [Category("Data Source")
        , DefaultValue(colorScheme.Blue)
        , Description("Select a color for the grid.")
        , WebBrowsable(true)
        , Personalizable(true)
        , WebDisplayName("Grid color scheme")]
        public colorScheme colorPickerChoice
        {
            get { return _colorScheme; }
            set { _colorScheme = value; }
        }
    }
}